﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using ApplicationGenerator.Versions.BusinessLogic;
using ApplicationGenerator.Versions.Forms;
using System.Collections.Generic;

namespace WorkflowLibrary
{
    /// <summary>
    /// Aktywność do wyświetlania formularzy
    /// </summary>
    public partial class ShowFormActivity : SequenceActivity, IActivityName//, ICustomTypeDescriptor
    {
        #region Events and event handlers

        public delegate void ShowFormEventHandler(object sender,
            ShowFormEventArgs e);
        private event ShowFormEventHandler formShownEvent;
        public event ShowFormEventHandler FormShown
        {
            add
            {
                formShownEvent += value;
            }
            remove
            {
                formShownEvent -= value;
            }
        }

        #endregion

        #region Construction

        public ShowFormActivity()
        {
            InitializeComponent();
            ActivityName = "Wyswietl formularz";
        }

        #endregion

        #region Dependency properties

        /// <summary>
        /// contains informations about form to be shown
        /// </summary>
        public static DependencyProperty FormVersionInfoProperty =
                    DependencyProperty.Register("FormVersionInfo",
                    typeof(VersionInfo),
                    typeof(ShowFormActivity));

        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [BrowsableAttribute(true)]
        [DescriptionAttribute("Informacja o wersji")]
        [CategoryAttribute("ShowForm Property")]
        [DisplayName("Docelowy formularz")]
        [TypeConverter(typeof(VersionConverter))]
        [Editor(typeof(VersionEditor), typeof(UITypeEditor))]
        [VersionType(VersionElement.Form)]
        public VersionInfo FormVerisonInfo
        {
            get
            {
                return ((VersionInfo)(base.GetValue(ShowFormActivity.FormVersionInfoProperty)));
            }
            set
            {
                base.SetValue(ShowFormActivity.FormVersionInfoProperty, value);
            }
        }

        #endregion

        #region Code methods

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            if (formShownEvent != null)
            {
                formShownEvent(null, new ShowFormEventArgs(FormVerisonInfo));
            }
        }

        #endregion

        #region ICustomTypeDescriptor Members

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection colAvailableCollection = TypeDescriptor.GetProperties(this, attributes, true);
            List<PropertyDescriptor> colDisplayProperties = new List<PropertyDescriptor>();
            colDisplayProperties.Add(colAvailableCollection["ActivityName"]);
            colDisplayProperties.Add(colAvailableCollection["FormVerisonInfo"]);

            return new PropertyDescriptorCollection(colDisplayProperties.ToArray());
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion

        #region IActivityName Members
        [Category("Dane podstawowe")]
        [DisplayName("Nazwa czynności")]
        [Description("Nazwa czynności przyjazna dla użytkownika")]
        public string ActivityName
        {
            get;
            set;
        }

        #endregion

    }

    #region Event Args class

    public class ShowFormEventArgs
    {
        private VersionInfo formVersionInfo;

        public ShowFormEventArgs(VersionInfo _formVersionInfo)
        {
            this.formVersionInfo = _formVersionInfo;
        }

        public VersionInfo FormVersionInfo
        {
            get { return formVersionInfo; }
            set { formVersionInfo = value; }
        }
    }

    #endregion
}
